const Mock = require('mockjs')

const List = []
const count = 100

for (let i = 0; i < count; i++) {
  List.push(Mock.mock({
    id: '@guid',
    name: '@cname',
    code: '@name(5, 15)',
    isEncode: '@boolean',
    value: '@ctitle',
    'category|1': ['分类1', '分类2', '分类3', '分类4'],
    'isEnabled|1': [true, false],
    updatedDate: '@datetime',
  }))
}

module.exports = [{
    url: '/admin/sysParameter/query',
    type: 'post',
    response: config => {
      // 请求参数
      let {
        query,
        start = 1,
        length = 10,
      } = config.body

      let {
        name,
        code
      } = query || {};

      // 条件过滤
      let mockList = List.filter(item => {
        if (name && item.name.indexOf(name) < 0) return false
        if (code && item.code.indexOf(code) < 0) return false
        return true
      })

      // 分页
      const pageList = mockList.filter((item, index) => index >= +length * +start && index < +length * (+start + 1))

      return {
        data: pageList,
        draw: 1,
        startIndex: 1,
        pageSize: 10,
        pages: 10,
        recordsFiltered: 100,
        recordsTotal: 100,
      }
    }
  },

  {
    url: '/admin/sysParameter/getEntity',
    type: 'post',
    response: config => {
      const {
        id
      } = config.query
      for (const article of List) {
        if (article.id === +id) {
          return article
        }
      }
    }
  },

  {
    url: '/admin/sysParameter/save',
    type: 'post',
    response: _ => {
      return {
        code: null,
        success: true,
        message: null,
      }
    }
  },

  {
    url: '/admin/sysParameter/delete',
    type: 'post',
    response: _ => {
      return {
        code: null,
        success: true,
        message: null,
      }
    }
  }
]
